// exports.GameGlobalVariable = void 0;
// var $bundleConfig = require("./BundleConfig");
// var $audioManager = require("./AudioManager");
// var $eventManager = require("./EventManager");
// var $resLoader = require("./ResLoader");
// var $evenType = require("./EvenType");
// var $excel = require("./Excel");
// var $excelUtil = require("./ExcelUtil");
// var $uI_BulletMove = require("./UI_BulletMove");
// var $uI_MosterLogic = require("./UI_MosterLogic");
// var $nodePoolMag = require("./NodePoolMag");
// var g = (function () {
//     function e() {}
//     e.initPeiZhi = function () {
//         e.huanyipi = this.getPeiZhiData("huanyipin");
//         e.allhave = this.getPeiZhiData("allhave");
//         e.shaodangtilicount = this.getPeiZhiData("saodangpower");
//         e.kaishitilicount = this.getPeiZhiData("kaishipower");
//         e.meirimianfeicount = this.getPeiZhiData("saodangfreecount");
//         e.houlevelunlock = this.getPeiZhiData("houlevellunlock");
//         e.wujintilicount = this.getPeiZhiData("wujintilicount");
//         e.wujinsaodangcount = this.getPeiZhiData("wujinsaodangtilicount");
//         e.wujinmeirifreecount = this.getPeiZhiData("wujinmeirifreecount");
//         e.wujinunlockcount = this.getPeiZhiData("wujinunlockcount");
//         e.addPowerTime = this.getPeiZhiData("addPowerTime");
//         e.bagVideoCellCount = this.getPeiZhiData("bagVideoCellCount");
//         e.severdatatime = this.getPeiZhiData("severdatatime");
//         e.putongfuhuocount = this.getPeiZhiData("putongfuhuocount");
//         e.wujinfuhuocount = this.getPeiZhiData("wujinfuhuocount");
//         e.liulianlanguabomb = this.getPeiZhiData("liulianlanguabomb");
//     };
//     e.getPeiZhiData = function (e) {
//         var t = $excelUtil.ExcelUtil.getSameData($excel.Excel.shuiguoconfig_all, "configkey", e);
//         if (t && t.length > 0) {
//             return t[0].value;
//         } else {
//             return (
//                 {
//                     severdatatime: 3,
//                     huanyipin: 10,
//                     allhave: 3,
//                     saodangpower: 5,
//                     kaishipower: 5,
//                     saodangfreecount: 3,
//                     houlevellunlock: 4,
//                     wujintilicount: 5,
//                     wujinsaodangtilicount: 5,
//                     wujinmeirifreecount: 3,
//                     addPowerTime: 20,
//                     bagVideoCellCount: 3,
//                     wujinunlockcount: 4,
//                     putongfuhuocount: 999999,
//                     wujinfuhuocount: 1,
//                     liulianlanguabomb: 0.4
//                 }[e] || 0
//             );
//         }
//     };
//     e.jiluAttack = function (e, t) {
//         var o = this.nowwShangHai[e];
//         if (!o) {
//             o = 0;
//         }
//         o += t;
//         this.nowwShangHai[e] = o;
//         $eventManager.EventMgr.dispatchEvent($evenType.EVENT_TYPE.GAME_SHANGHAI_TONGJI);
//     };
//     e.cleanAttack = function () {
//         this.nowwShangHai = {};
//     };
//     e.playHitJianShu = function () {
//         if (new Date().getTime() - this.offtime1 > 3e3) {
//             console.log("播放冻死梨子音效");
//             $audioManager.AudioMgr.playEffect("sound/冻梨音效", !1, $bundleConfig.BundleNames.Game);
//             this.offtime1 = new Date().getTime();
//         }
//     };
//     e.playHitLiulian = function () {
//         if (new Date().getTime() - this.offtimeLiuLian > 300 * Math.random()) {
//             $audioManager.AudioMgr.playEffect("sound/榴莲打击", !1, $bundleConfig.BundleNames.Game);
//             this.offtimeLiuLian = new Date().getTime();
//         }
//     };
//     e.playHit = function () {
//         if (new Date().getTime() - this.offtime > 500 * Math.random()) {
//             $audioManager.AudioMgr.playEffect("sound/hit", !1, $bundleConfig.BundleNames.Game);
//             this.offtime = new Date().getTime();
//         }
//     };
//     e.playHitBomb1 = function () {
//         if (new Date().getTime() - this.offbomb1time > 500 * Math.random()) {
//             $audioManager.AudioMgr.playEffect("sound/南瓜炸弹", !1, $bundleConfig.BundleNames.Game);
//             this.offbomb1time = new Date().getTime();
//         }
//     };
//     e.playHitBomb2 = function () {
//         if (new Date().getTime() - this.offbomb2time > 500 * Math.random()) {
//             $audioManager.AudioMgr.playEffect("sound/西瓜爆炸", !1, $bundleConfig.BundleNames.Game);
//             this.offbomb2time = new Date().getTime();
//         }
//     };
//     e.isTeShuLevel = function () {
//         if (-1 == e.nowlevel) {
//             return 1;
//         } else {
//             return 2;
//         }
//     };
//     e.subHuanYiPi = function () {
//         this.huanyipi -= 1;
//     };
//     e.suballHave = function () {
//         this.allhave -= 1;
//     };
//     e.getSceneId = function () {
//         return (
//             -1 == this.nowlevel
//                 ? $excel.Excel.shuiguotiaozhan(this.teshulevel)
//                 : $excel.Excel.shuiguoguanqia(this.nowlevel)
//         ).dipicture;
//     };
//     e.addNowLevelHuoBi = function (e) {
//         this.nowlevelhuobi += e;
//     };
//     e.subNowLevelHuoBi = function (e) {
//         this.nowlevelhuobi -= e;
//     };
//     e.paixuMoster = function (e) {
//         var t = e.getComponentsInChildren($uI_MosterLogic.default);
//         var o = [];
//         var n = [];
//         for (var i = 0; i < t.length; i++) {
//             var a = t[i];
//             if (!a.isDie) {
//                 var r = cc.Vec2.subtract(cc.v2(), cc.v2(), a.node.getPosition());
//                 var c = Math.atan2(r.y, r.x);
//                 var s = (180 / Math.PI) * c + 180;
//                 if (s >= 0 && s <= 90) {
//                     o.push({
//                         jiaoben: s,
//                         script: a
//                     });
//                 } else {
//                     n.push({
//                         jiaoben: s,
//                         script: a
//                     });
//                 }
//             }
//         }
//         o.sort(function (e, t) {
//             if (e.jiaoben > t.jiaoben) {
//                 return -1;
//             } else {
//                 return 1;
//             }
//         });
//         n.sort(function (e, t) {
//             if (e.jiaoben > t.jiaoben) {
//                 return -1;
//             } else {
//                 return 1;
//             }
//         });
//         var l = [];
//         return (l = l.concat(o)).concat(n);
//     };
//     e.LanGuaPaiXu = function (e, t) {
//         var o = [];
//         var n = [];
//         for (var i = 0; i < e.length; i++) {
//             var a = e[i];
//             a.setBoxState(!1);
//             var r = cc.Vec2.subtract(cc.v2(), cc.v2(), a.node.getPosition());
//             var c = Math.atan2(r.y, r.x);
//             var s = (180 / Math.PI) * c + 180;
//             if (s >= 0 && s <= 90) {
//                 o.push({
//                     jiaoben: s,
//                     script: a
//                 });
//             } else {
//                 n.push({
//                     jiaoben: s,
//                     script: a
//                 });
//             }
//         }
//         o.sort(function (e, t) {
//             if (e.jiaoben > t.jiaoben) {
//                 return -1;
//             } else {
//                 return 1;
//             }
//         });
//         n.sort(function (e, t) {
//             if (e.jiaoben > t.jiaoben) {
//                 return -1;
//             } else {
//                 return 1;
//             }
//         });
//         var l = this.paixuMoster(t)[0];
//         if (l && (o.length > 0 || n.length > 0)) {
//             l.jiaoben > 0 && l.jiaoben < 90
//                 ? (n.forEach(function (e) {
//                       e.script.setBoxState(!0);
//                   }),
//                   o.forEach(function (e) {
//                       if (e.jiaoben > l.jiaoben + 20) {
//                           e.script.setBoxState(!1);
//                       } else {
//                           e.script.setBoxState(!0);
//                       }
//                   }))
//                 : (o.forEach(function (e) {
//                       e.script.setBoxState(!1);
//                   }),
//                   n.forEach(function (e) {
//                       if (e.jiaoben > l.jiaoben + 20) {
//                           e.script.setBoxState(!1);
//                       } else {
//                           e.script.setBoxState(!0);
//                       }
//                   }));
//         }
//     };
//     e.addMoveBullet = function (e) {
//         this.movebullets.push(e);
//         if (-1 == this.ishavetime) {
//             this.sendMoveBullet();
//             this.ishavetime = setInterval(this.sendMoveBullet.bind(this), 200);
//         }
//     };
//     e.sendMoveBullet = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var t;
//             var o;
//             var n;
//             var r;
//             return __generator(this, function (i) {
//                 switch (i.label) {
//                     case 0:
//                         if (this.movebullets.length > 0) {
//                             return (
//                                 (t = this.movebullets.shift()),
//                                 (o = $nodePoolMag.NodePoolMag.instance.getNode("bullet_3"))
//                                     ? [3, 2]
//                                     : [
//                                           4,
//                                           $resLoader.default.loadRes(
//                                               $bundleConfig.BundleNames.Game,
//                                               "prefabs/bullet/bullet3",
//                                               cc.Prefab
//                                           )
//                                       ]
//                             );
//                         } else {
//                             return [3, 3];
//                         }
//                     case 1:
//                         n = i.sent();
//                         $nodePoolMag.NodePoolMag.instance.createNodeCount("bullet_3", 1, n);
//                         o = $nodePoolMag.NodePoolMag.instance.getNode("bullet_3");
//                         i.label = 2;
//                     case 2:
//                         r = e.paths.children[5].convertToWorldSpaceAR(cc.v2());
//                         r = e.liulianNode.convertToNodeSpaceAR(r);
//                         o.x = r.x;
//                         o.y = r.y;
//                         o.scale = 1;
//                         e.liulianNode.addChild(o);
//                         setTimeout(function () {
//                             o.getComponent($uI_BulletMove.default).initBullet(t);
//                         }, 10);
//                         return [3, 4];
//                     case 3:
//                         clearInterval(this.ishavetime);
//                         this.ishavetime = -1;
//                         i.label = 4;
//                     case 4:
//                         return [2];
//                 }
//             });
//         });
//     };
//     e.getMosterDis = function (e, t) {
//         var o = e.node.x;
//         var n = e.node.y;
//         var i = e.moveindex;
//         var a = t.children;
//         switch (e.moveindex) {
//             case 1:
//                 if ((o += 100) > a[0].x) {
//                     o = a[0].x;
//                 }
//                 break;
//             case 2:
//                 if ((n += 100) > a[1].y) {
//                     var r = n - a[1].y;
//                     n = a[1].y;
//                     o = a[1].x + r;
//                     i -= 1;
//                 }
//                 break;
//             case 3:
//                 if ((o -= 100) < a[2].x) {
//                     r = Math.abs(o - a[2].x);
//                     o = a[2].x;
//                     n = a[2].y + r;
//                     i -= 1;
//                 }
//                 break;
//             case 4:
//                 if ((n -= 100) < a[3].y) {
//                     r = Math.abs(n - a[3].y);
//                     o = a[3].x - r;
//                     n = a[3].y;
//                     i -= 1;
//                 }
//                 break;
//             case 5:
//                 if ((o += 100) > a[4].x) {
//                     r = Math.abs(o + a[4].x);
//                     o = a[4].x;
//                     n = a[4].y - r;
//                     i -= 1;
//                 }
//         }
//         return {
//             pos: cc.v2(o, n),
//             tagindex: i
//         };
//     };
//     e.generateEquidistantCoordinates = function (e, t, o, n, i, a, r, c) {
//         if (void 0 === a) {
//             a = 1;
//         }
//         var s = [];
//         for (var l = 0; l < i; l++) {
//             var u = e;
//             if (1 == a && (u = e + o * l) > c) {
//                 break;
//             }
//             if (2 == a && (u = e - o * l) < c) {
//                 break;
//             }
//             var d = t;
//             if (3 == a && (d = t + n * l) > c) {
//                 break;
//             }
//             if (4 == a && (d = t - n * l) < c) {
//                 break;
//             }
//             s.push({
//                 pos: cc.v2(u, d),
//                 tagindex: r
//             });
//         }
//         return s;
//     };
//     e.fenlieCreateMoster = function (e, t) {
//         var o = t.children;
//         var n = 2;
//         var i = [];
//         for (var a = e.moveindex; a > 0; a--) {
//             var r = e.node.x;
//             var c = e.node.y;
//             var s = 0;
//             var l = 0;
//             var u = [];
//             switch (a) {
//                 case 5:
//                     s = o[a].y;
//                     l = o[a].x;
//                     if (a == e.moveindex) {
//                         l = r;
//                     }
//                     u = this.generateEquidistantCoordinates(l, s, 50, 0, n, 1, a, o[4].x);
//                     break;
//                 case 1:
//                     s = o[a].y;
//                     l = o[a].x;
//                     if (a == e.moveindex) {
//                         l = r;
//                     }
//                     u = this.generateEquidistantCoordinates(l, s, 50, 0, n, 1, a, o[0].x);
//                     break;
//                 case 2:
//                     s = o[a].y;
//                     l = o[a].x;
//                     if (a == e.moveindex) {
//                         s = c;
//                     }
//                     u = this.generateEquidistantCoordinates(l, s, 0, 50, n, 3, a, o[1].y);
//                     break;
//                 case 3:
//                     s = o[a].y;
//                     l = o[a].x;
//                     if (a == e.moveindex) {
//                         l = r;
//                     }
//                     u = this.generateEquidistantCoordinates(l, s, 50, 0, n, 2, a, o[2].x);
//                     break;
//                 case 4:
//                     s = o[a].y;
//                     l = o[a].x;
//                     if (a == e.moveindex) {
//                         s = c;
//                     }
//                     u = this.generateEquidistantCoordinates(l, s, 0, 50, n, 4, a, o[3].y);
//             }
//             if (u.length >= n) {
//                 i = i.concat(u.slice(0, n));
//                 break;
//             }
//             n -= u.length;
//             i = i.concat(u);
//         }
//         return i;
//     };
//     e.nowlevel = 1;
//     e.teshulevel = 1;
//     e.nowlevelhuobi = 0;
//     e.selfMap = {};
//     e.bulletNode = null;
//     e.languaNode = null;
//     e.liulianNode = null;
//     e.mosterNode = null;
//     e.effNode = null;
//     e.paths = null;
//     e.huanyipi = 0;
//     e.allhave = 0;
//     e.shaodangtilicount = 0;
//     e.kaishitilicount = 0;
//     e.meirimianfeicount = 0;
//     e.houlevelunlock = 0;
//     e.wujintilicount = 0;
//     e.wujinsaodangcount = 0;
//     e.wujinmeirifreecount = 0;
//     e.wujinunlockcount = 0;
//     e.putongfuhuocount = 0;
//     e.wujinfuhuocount = 0;
//     e.liulianlanguabomb = 0;
//     e.jiasuState = 1;
//     e.isPad = !1;
//     e.severdatatime = 3;
//     e.addPowerTime = 0;
//     e.bagVideoCellCount = 0;
//     e.adddimond = 0;
//     e.addjinbi = 0;
//     e.addpower = 0;
//     e.normalMaxLevel = 4;
//     e.zaZhongMaxLevel = 5;
//     e.viewofftime = 0;
//     e.isFirstGuide = !1;
//     e.nowwShangHai = {};
//     e.offtime1 = 0;
//     e.offtimeLiuLian = 0;
//     e.offtime = 0;
//     e.offbomb1time = 0;
//     e.offbomb2time = 0;
//     e.movebullets = [];
//     e.ishavetime = -1;
//     return e;
// })();
// exports.GameGlobalVariable = g;


import { BundleNames } from "./BundleConfig";
import { AudioMgr } from "./AudioManager";
import { EventMgrInstance } from "./EventManager";
import ResLoader from "./ResLoader";
import { EVENT_TYPE } from "./EvenType";
import { Excel } from "./Excel";
import { ExcelUtil } from "./ExcelUtil";
import ViewBulletMove from "./UI_BulletMove";
import ViewMosterLogic from "./UI_MosterLogic";
import { NodePoolMag } from "./NodePoolMag";

/**
 * 游戏全局变量类
 * 建议类名: GameGlobalVariables
 */
class GameGlobalVariable {
    static nowlevel: number = 1;
    static teshulevel: number = 1;
    static nowlevelhuobi: number = 0;
    static selfMap: Record<string, any> = {};
    static bulletNode: cc.Node | null = null;
    static languaNode: cc.Node | null = null;
    static liulianNode: cc.Node | null = null;
    static mosterNode: cc.Node | null = null;
    static effNode: cc.Node | null = null;
    static paths: cc.Node | null = null;
    static huanyipi: number = 0;
    static allhave: number = 0;
    static shaodangtilicount: number = 0;
    static kaishitilicount: number = 0;
    static meirimianfeicount: number = 0;
    static houlevelunlock: number = 0;
    static wujintilicount: number = 0;
    static wujinsaodangcount: number = 0;
    static wujinmeirifreecount: number = 0;
    static wujinunlockcount: number = 0;
    static putongfuhuocount: number = 0;
    static wujinfuhuocount: number = 0;
    static liulianlanguabomb: number = 0;
    static jiasuState: number = 1;
    static isPad: boolean = false;
    static severdatatime: number = 3;
    static addPowerTime: number = 0;
    static bagVideoCellCount: number = 0;
    static adddimond: number = 0;
    static addjinbi: number = 0;
    static addpower: number = 0;
    static normalMaxLevel: number = 4;
    static zaZhongMaxLevel: number = 5;
    static viewofftime: number = 0;
    static isFirstGuide: boolean = false;
    static nowwShangHai: Record<string, number> = {};
    static offtime1: number = 0;
    static offtimeLiuLian: number = 0;
    static offtime: number = 0;
    static offbomb1time: number = 0;
    static offbomb2time: number = 0;
    static movebullets: any[] = [];
    static ishavetime: any = -1;

    /**
     * 初始化配置数据
     * 建议方法名: initializeConfiguration
     */
    static initializeConfig(): void {
        this.huanyipi = this.getConfigData("huanyipin");
        this.allhave = this.getConfigData("allhave");
        this.shaodangtilicount = this.getConfigData("saodangpower");
        this.kaishitilicount = this.getConfigData("kaishipower");
        this.meirimianfeicount = this.getConfigData("saodangfreecount");
        this.houlevelunlock = this.getConfigData("houlevellunlock");
        this.wujintilicount = this.getConfigData("wujintilicount");
        this.wujinsaodangcount = this.getConfigData("wujinsaodangtilicount");
        this.wujinmeirifreecount = this.getConfigData("wujinmeirifreecount");
        this.wujinunlockcount = this.getConfigData("wujinunlockcount");
        this.addPowerTime = this.getConfigData("addPowerTime");
        this.bagVideoCellCount = this.getConfigData("bagVideoCellCount");
        this.severdatatime = this.getConfigData("severdatatime");
        this.putongfuhuocount = this.getConfigData("putongfuhuocount");
        this.wujinfuhuocount = this.getConfigData("wujinfuhuocount");
        this.liulianlanguabomb = this.getConfigData("liulianlanguabomb");
    }

    /**
     * 获取配置数据
     * @param key 配置键
     * @returns 配置值
     * 建议方法名: getConfigurationData
     */
    static getConfigData(key: string): number {
        const data = ExcelUtil.getMatchingData(Excel.shuiguoconfig_all, "configkey", key);
        if (data && data.length > 0) {
            return data[0].value;
        } else {
            return (
                {
                    severdatatime: 3,
                    huanyipin: 10,
                    allhave: 3,
                    saodangpower: 5,
                    kaishipower: 5,
                    saodangfreecount: 3,
                    houlevellunlock: 4,
                    wujintilicount: 5,
                    wujinsaodangtilicount: 5,
                    wujinmeirifreecount: 3,
                    addPowerTime: 20,
                    bagVideoCellCount: 3,
                    wujinunlockcount: 4,
                    putongfuhuocount: 999999,
                    wujinfuhuocount: 1,
                    liulianlanguabomb: 0.4
                }[key] || 0
            );
        }
    }

    /**
     * 记录攻击
     * @param key 攻击键
     * @param damage 伤害值
     * 建议方法名: recordAttack
     */
    static recordAttack(key: string, damage: number): void {
        let currentDamage = this.nowwShangHai[key] || 0;
        currentDamage += damage;
        this.nowwShangHai[key] = currentDamage;
        EventMgrInstance.emitEvent(EVENT_TYPE.GAME_SHANGHAI_TONGJI);
    }

    /**
     * 清除攻击记录
     * 建议方法名: clearAttackRecords
     */
    static clearAttackRecords(): void {
        this.nowwShangHai = {};
    }

    /**
     * 播放击中音效
     * 建议方法名: playHitSoundEffect
     */
    static playHitSoundEffect(): void {
        if (new Date().getTime() - this.offtime1 > 3000) {
            //console.log("播放冻死梨子音效");
            AudioMgr.playSoundEffect("sound/冻梨音效", false, BundleNames.Game);
            this.offtime1 = new Date().getTime();
        }
    }

    /**
     * 播放击中榴莲音效
     * 建议方法名: playHitDurianSoundEffect
     */
    static playHitDurianSoundEffect(): void {
        if (new Date().getTime() - this.offtimeLiuLian > 300 * Math.random()) {
            AudioMgr.playSoundEffect("sound/榴莲打击", false, BundleNames.Game);
            this.offtimeLiuLian = new Date().getTime();
        }
    }

    /**
     * 播放击中音效
     * 建议方法名: playHitSound
     */
    static playHitSound(): void {
        if (new Date().getTime() - this.offtime > 500 * Math.random()) {
            AudioMgr.playSoundEffect("sound/hit", false, BundleNames.Game);
            this.offtime = new Date().getTime();
        }
    }

    /**
     * 播放炸弹1音效
     * 建议方法名: playBomb1SoundEffect
     */
    static playBomb1SoundEffect(): void {
        if (new Date().getTime() - this.offbomb1time > 500 * Math.random()) {
            AudioMgr.playSoundEffect("sound/南瓜炸弹", false, BundleNames.Game);
            this.offbomb1time = new Date().getTime();
        }
    }

    /**
     * 播放炸弹2音效
     * 建议方法名: playBomb2SoundEffect
     */
    static playBomb2SoundEffect(): void {
        if (new Date().getTime() - this.offbomb2time > 500 * Math.random()) {
            AudioMgr.playSoundEffect("sound/西瓜爆炸", false, BundleNames.Game);
            this.offbomb2time = new Date().getTime();
        }
    }

    /**
     * 判断是否特殊关卡
     * @returns 特殊关卡标识
     * 建议方法名: isSpecialLevel
     */
    static isSpecialLevel(): number {
        return this.nowlevel === -1 ? 1 : 2;
    }

    /**
     * 减少换一批次数
     * 建议方法名: decrementHuanYiPi
     */
    static decrementHuanYiPi(): void {
        this.huanyipi -= 1;
    }

    /**
     * 减少全部拥有次数
     * 建议方法名: decrementAllHave
     */
    static decrementAllHave(): void {
        this.allhave -= 1;
    }

    /**
     * 获取场景ID
     * @returns 场景ID
     * 建议方法名: getSceneId
     */
    static getSceneId(): number {
        return (
            this.nowlevel === -1
                ? Excel.shuiguotiaozhan(this.teshulevel)
                : Excel.shuiguoguanqia(this.nowlevel)
        ).dipicture;
    }

    /**
     * 增加当前关卡货币
     * @param amount 增加的货币数量
     * 建议方法名: addCurrentLevelCurrency
     */
    static addCurrentLevelCurrency(amount: number): void {
        this.nowlevelhuobi += amount;
    }

    /**
     * 减少当前关卡货币
     * @param amount 减少的货币数量
     * 建议方法名: subtractCurrentLevelCurrency
     */
    static subtractCurrentLevelCurrency(amount: number): void {
        this.nowlevelhuobi -= amount;
    }

    /**
     * 排序怪物
     * @param parentNode 父节点
     * @returns 排序后的怪物数组
     * 建议方法名: sortMonsters
     */
    static sortMonsters(parentNode: cc.Node): any[] {
        const monsterComponents = parentNode.getComponentsInChildren(ViewMosterLogic);
        const quadrant1: any[] = [];
        const quadrant2: any[] = [];

        for (const monster of monsterComponents) {
            if (!monster.isDie) {
                const position = cc.Vec2.subtract(cc.v2(), cc.v2(), monster.node.getPosition());
                const angle = (180 / Math.PI) * Math.atan2(position.y, position.x) + 180;
                if (angle >= 0 && angle <= 90) {
                    quadrant1.push({ angle, script: monster });
                } else {
                    quadrant2.push({ angle, script: monster });
                }
            }
        }

        quadrant1.sort((a, b) => b.angle - a.angle);
        quadrant2.sort((a, b) => b.angle - a.angle);

        return [...quadrant1, ...quadrant2];
    }

    /**
     * 排序蓝瓜
     * @param blueMelons 蓝瓜数组
     * @param parentNode 父节点
     * 建议方法名: sortBlueMelons
     */
    static sortBlueMelons(blueMelons: any[], parentNode: cc.Node): void {
        const quadrant1: any[] = [];
        const quadrant2: any[] = [];

        for (const melon of blueMelons) {
            melon.setBoxState(false);
            const position = cc.Vec2.subtract(cc.v2(), cc.v2(), melon.node.getPosition());
            const angle = (180 / Math.PI) * Math.atan2(position.y, position.x) + 180;
            if (angle >= 0 && angle <= 90) {
                quadrant1.push({ angle, script: melon });
            } else {
                quadrant2.push({ angle, script: melon });
            }
        }

        quadrant1.sort((a, b) => b.angle - a.angle);
        quadrant2.sort((a, b) => b.angle - a.angle);

        const sortedMonsters = this.sortMonsters(parentNode)[0];
        if (sortedMonsters && (quadrant1.length > 0 || quadrant2.length > 0)) {
            if (sortedMonsters.angle > 0 && sortedMonsters.angle < 90) {
                quadrant2.forEach(melon => melon.script.setBoxState(true));
                quadrant1.forEach(melon => {
                    if (melon.angle > sortedMonsters.angle + 20) {
                        melon.script.setBoxState(false);
                    } else {
                        melon.script.setBoxState(true);
                    }
                });
            } else {
                quadrant1.forEach(melon => melon.script.setBoxState(false));
                quadrant2.forEach(melon => {
                    if (melon.angle > sortedMonsters.angle + 20) {
                        melon.script.setBoxState(false);
                    } else {
                        melon.script.setBoxState(true);
                    }
                });
            }
        }
    }

    /**
     * 添加移动子弹
     * @param bullet 子弹对象
     * 建议方法名: addMovingBullet
     */
    static addMovingBullet(bullet: any): void {
        this.movebullets.push(bullet);
        if (this.ishavetime === -1) {
            this.sendMovingBullet();
            this.ishavetime = setInterval(this.sendMovingBullet.bind(this), 200);
        }
    }

    /**
     * 发送移动子弹
     * 建议方法名: sendMovingBullet
     */
    static async sendMovingBullet(): Promise<void> {
        if (this.movebullets.length > 0) {
            const bulletData = this.movebullets.shift();
            let bulletNode = NodePoolMag.instance.getNodeFromPool("bullet_3");
            if (!bulletNode) {
                const prefab = await ResLoader.loadRes(
                    BundleNames.Game,
                    "prefabs/bullet/bullet3",
                    cc.Prefab
                );
                NodePoolMag.instance.createNodePool("bullet_3", 1, prefab);
                bulletNode = NodePoolMag.instance.getNodeFromPool("bullet_3");
            }
            const worldPosition = bulletData.paths.children[5].convertToWorldSpaceAR(cc.v2());
            const localPosition = bulletData.liulianNode.convertToNodeSpaceAR(worldPosition);
            bulletNode.setPosition(localPosition);
            bulletNode.scale = 1;
            bulletData.liulianNode.addChild(bulletNode);
            setTimeout(() => {
                bulletNode.getComponent(ViewBulletMove).initializeBullet(bulletData);
            }, 10);
        } else {
            clearInterval(this.ishavetime);
            this.ishavetime = -1;
        }
    }

    /**
     * 获取怪物距离
     * @param monster 怪物对象
     * @param parentNode 父节点
     * @returns 怪物距离信息
     * 建议方法名: getMonsterDistance
     */
    static getMonsterDistance(monster: any, parentNode: cc.Node): { pos: cc.Vec2, tagindex: number } {
        let x = monster.node.x;
        let y = monster.node.y;
        let moveIndex = monster.moveindex;
        const children = parentNode.children;

        switch (moveIndex) {
            case 1:
                x += 100;
                if (x > children[0].x) x = children[0].x;
                break;
            case 2:
                y += 100;
                if (y > children[1].y) {
                    const diff = y - children[1].y;
                    y = children[1].y;
                    x = children[1].x + diff;
                    moveIndex -= 1;
                }
                break;
            case 3:
                x -= 100;
                if (x < children[2].x) {
                    const diff = Math.abs(x - children[2].x);
                    x = children[2].x;
                    y = children[2].y + diff;
                    moveIndex -= 1;
                }
                break;
            case 4:
                y -= 100;
                if (y < children[3].y) {
                    const diff = Math.abs(y - children[3].y);
                    x = children[3].x - diff;
                    y = children[3].y;
                    moveIndex -= 1;
                }
                break;
            case 5:
                x += 100;
                if (x > children[4].x) {
                    const diff = Math.abs(x + children[4].x);
                    x = children[4].x;
                    y = children[4].y - diff;
                    moveIndex -= 1;
                }
                break;
        }

        return {
            pos: cc.v2(x, y),
            tagindex: moveIndex
        };
    }

    /**
     * 生成等距坐标
     * @param startX 起始X坐标
     * @param startY 起始Y坐标
     * @param offsetX X轴偏移量
     * @param offsetY Y轴偏移量
     * @param count 坐标数量
     * @param direction 方向
     * @param tagIndex 标签索引
     * @param limit 限制值
     * @returns 等距坐标数组
     * 建议方法名: generateEquidistantCoordinates
     */
    static generateEquidistantCoordinates(
        startX: number,
        startY: number,
        offsetX: number,
        offsetY: number,
        count: number,
        direction: number = 1,
        tagIndex: number,
        limit: number
    ): { pos: cc.Vec2, tagindex: number }[] {
        const coordinates: { pos: cc.Vec2, tagindex: number }[] = [];
        for (let i = 0; i < count; i++) {
            let x = startX;
            let y = startY;
            if (direction === 1 && (x = startX + offsetX * i) > limit) break;
            if (direction === 2 && (x = startX - offsetX * i) < limit) break;
            if (direction === 3 && (y = startY + offsetY * i) > limit) break;
            if (direction === 4 && (y = startY - offsetY * i) < limit) break;
            coordinates.push({ pos: cc.v2(x, y), tagindex: tagIndex });
        }
        return coordinates;
    }

    /**
     * 分裂创建怪物
     * @param monster 怪物对象
     * @param parentNode 父节点
     * @returns 分裂后的怪物坐标数组
     * 建议方法名: splitCreateMonster
     */
    static splitCreateMonster(monster: any, parentNode: cc.Node): { pos: cc.Vec2, tagindex: number }[] {
        const children = parentNode.children;
        let remainingCount = 2;
        let coordinates: { pos: cc.Vec2, tagindex: number }[] = [];

        for (let i = monster.moveindex; i > 0; i--) {
            let x = monster.node.x;
            let y = monster.node.y;
            let targetX = 0;
            let targetY = 0;
            let newCoordinates: { pos: cc.Vec2, tagindex: number }[] = [];

            switch (i) {
                case 5:
                    targetY = children[i].y;
                    targetX = children[i].x;
                    if (i === monster.moveindex) targetX = x;
                    newCoordinates = this.generateEquidistantCoordinates(targetX, targetY, 50, 0, remainingCount, 1, i, children[4].x);
                    break;
                case 1:
                    targetY = children[i].y;
                    targetX = children[i].x;
                    if (i === monster.moveindex) targetX = x;
                    newCoordinates = this.generateEquidistantCoordinates(targetX, targetY, 50, 0, remainingCount, 1, i, children[0].x);
                    break;
                case 2:
                    targetY = children[i].y;
                    targetX = children[i].x;
                    if (i === monster.moveindex) targetY = y;
                    newCoordinates = this.generateEquidistantCoordinates(targetX, targetY, 0, 50, remainingCount, 3, i, children[1].y);
                    break;
                case 3:
                    targetY = children[i].y;
                    targetX = children[i].x;
                    if (i === monster.moveindex) targetX = x;
                    newCoordinates = this.generateEquidistantCoordinates(targetX, targetY, 50, 0, remainingCount, 2, i, children[2].x);
                    break;
                case 4:
                    targetY = children[i].y;
                    targetX = children[i].x;
                    if (i === monster.moveindex) targetY = y;
                    newCoordinates = this.generateEquidistantCoordinates(targetX, targetY, 0, 50, remainingCount, 4, i, children[3].y);
                    break;
            }

            if (newCoordinates.length >= remainingCount) {
                coordinates = coordinates.concat(newCoordinates.slice(0, remainingCount));
                break;
            }

            remainingCount -= newCoordinates.length;
            coordinates = coordinates.concat(newCoordinates);
        }

        return coordinates;
    }
}

export { GameGlobalVariable };